package com.animee.tally.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.animee.tally.R;
import com.animee.tally.db.AccountBean;
import java.util.Calendar;
import java.util.List;
/*
* 1/创建一个类，继承BaseAdapter
* 2.重写4个方法
* 3.将数据源List，以及Context，通过构造方法进行传递
* */
public class AccountAdapter extends BaseAdapter {
    Context context;
    List<AccountBean>mDatas;
    int year,month,day;
    public AccountAdapter(Context context, List<AccountBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
        Calendar calendar = Calendar.getInstance();
         year = calendar.get(Calendar.YEAR);
         month = calendar.get(Calendar.MONTH)+1;
         day = calendar.get(Calendar.DAY_OF_MONTH);
    }
    // 返回集合的长度
    @Override
    public int getCount() {
        return mDatas.size();
    }
   // 返回指定位置的数据
    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    // 返回指定位置的View对象
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        //1.判断内存当中是否有可以进行复用的view
        if (convertView == null) {   //没有复用，需要创建
            convertView = LayoutInflater.from(context).inflate(R.layout.item_mainlv,null);  //将布局转换成view对象
            holder = new ViewHolder(convertView);   // 查找view当中包括的控件，创建管理者对象
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        // 2.获取当前位置的数据
        AccountBean accountBean = mDatas.get(position);
        // 3.将数据显示在控件上
        holder.iv.setImageResource(accountBean.getsImageId());
        //查看记账记录是否为今天
        if (accountBean.getYear()==year&&accountBean.getMonth()==month&&accountBean.getDay()==day) {
            String time = accountBean.getTime().split(" ")[1];
            holder.timeTv.setText("今天 "+time);
        }else {
            String time = accountBean.getTime();
            holder.timeTv.setText(time);
        }
        holder.moneyTv.setText("￥"+accountBean.getMoney());
        holder.beizhuTv.setText(accountBean.getBeizhu());
        holder.typeTv.setText(accountBean.getTypename());
        return convertView;
    }
    // 将item布局当中需要使用的控件，进行统一的管理
    class ViewHolder{
        ImageView iv;
        TextView typeTv,beizhuTv,timeTv,moneyTv;
        public ViewHolder(View view){
            iv = view.findViewById(R.id.item_mainlv_iv);
            typeTv = view.findViewById(R.id.item_mainlv_tv_title);
            beizhuTv = view.findViewById(R.id.item_mainlv_tv_beizhu);
            timeTv = view.findViewById(R.id.item_mainlv_tv_time);
            moneyTv = view.findViewById(R.id.item_mainlv_tv_money);
        }
    }
}
